//
//  ESGTeamInquiryController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/8.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class ESGTeamInquiryController: BaseViewController {
    
    private var page = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "团队问诊"
        setupRefresh()
    }
    
    private var viewModel: ESGTeamInquiryViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 0, right: 0)
            tableView.rowHeight = 170
            tableView.estimatedRowHeight = UITableView.automaticDimension
            tableView.backgroundColor = .background
            tableView.tableFooterView = UIView()
            tableView.register(ESGTeamInquiryViewCell.nibForCell(),
                               forCellReuseIdentifier: ESGTeamInquiryViewCell.reuseIdentifier())
        }
    }
}

extension ESGTeamInquiryController {
    private func setupRefresh() {
        tableView.mj_header = refreshHeader(action: #selector(loadData))
        tableView.mj_header?.beginRefreshing()
        tableView.mj_footer = noMoreDataFooter("没有更多医生团队了")
    }
    
    private func endRefresh() {
        tableView.mj_header?.endRefreshing()
//        tableView.mj_footer?.endRefreshing()
    }
    
    @objc private func loadData() {
        page = 1
        handleData(loadMore: false)
    }
    
    @objc private func loadMoreData() {
        page += 1
        handleData(loadMore: true)
    }
    
    private func handleData(loadMore: Bool) {
        ESGTeamInquiryViewModel.getTeamInquiryList { (models, errorMsg, code) in
            self.endRefresh()
            switch code {
            case .failure:
                if loadMore { self.page -= 1 }
                HUDTool.showError(message: errorMsg)
            case .success:
                if !loadMore {
                    self.viewModel = ESGTeamInquiryViewModel(models: models)
                } else {
                    self.viewModel?.loadMore(with: models)
                    self.tableView.reloadData()
                }
            }
        }
    }
}

// MARK: - dataSource & delegate
extension ESGTeamInquiryController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.numberOfRows ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ESGTeamInquiryViewCell.reuseIdentifier(),
                                                 for: indexPath) as! ESGTeamInquiryViewCell
        if let vm = viewModel?.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        
        if let vm = viewModel?.viewModel(for: indexPath) {
            pushTeamInpuiryDetailVc(with: vm)
        }
    }
}

extension ESGTeamInquiryController {
    private func pushTeamInpuiryDetailVc(with vm: ESGTeamInquiryViewModel.RowViewModel) {
        let detailVc = ESGTeamInquiryDetailController(teamId: vm.teamId)
        navigationController?.pushViewController(detailVc, animated: true)
    }
}
